package com.azzcs.emailservicealibaba.service.impl;

import com.azzcs.emailservicecore.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: wzg
 * @Date: 2021/2/22 下午5:25
 */
@Slf4j
@Service
@RefreshScope
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    private ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());



    @Override
    public void sendCreateCodeEmail(String email, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromEmail);
        message.setTo(email);
        message.setSubject("主题：验证码");
        message.setText(code);
        log.info("from"+fromEmail+",to:"+email+",send message:"+message);
        executorService.execute(()->{
            mailSender.send(message);
        });
    }
}
